package com.yhny.lykj.yuhannongye.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.widget.CompoundButton;
import android.widget.RadioButton;

import com.longsichao.lscframe.view.LSCToast;
import com.yhny.lykj.yuhannongye.MyApplication;
import com.yhny.lykj.yuhannongye.R;
import com.yhny.lykj.yuhannongye.app.AppManager;
import com.yhny.lykj.yuhannongye.app.BaseActivity;
import com.yhny.lykj.yuhannongye.app.BaseFragment;
import com.yhny.lykj.yuhannongye.app.UserConfig;


public class MainActivity extends BaseActivity implements CompoundButton.OnCheckedChangeListener {
    private int BOTTOM_TYPE = BOTTOM_VIP;
    public static MainActivity mainActivity;

    private RadioButton bottomVipView;
    private RadioButton bottomHomeView;
    private RadioButton bottomCarView;
    private RadioButton bottomShopView;

    private UserFragment vipFragment = null;
    private HomeFragment homeFragment = null;
    private ShopFragment shopFragment = null;
    private CarFragment carFragment = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mainActivity = this;
        bottomVipView = (RadioButton) findViewById(R.id.navigation_tabs_vip);
        bottomVipView.setOnCheckedChangeListener(this);
        bottomHomeView = (RadioButton) findViewById(R.id.navigation_tabs_home);
        bottomHomeView.setOnCheckedChangeListener(this);
        bottomCarView = (RadioButton) findViewById(R.id.navigation_tabs_car);
        bottomCarView.setOnCheckedChangeListener(this);
        bottomShopView = (RadioButton) findViewById(R.id.navigation_tabs_shop);
        bottomShopView.setOnCheckedChangeListener(this);

        setFragments(BOTTOM_HOME);
    }
    /**
     * 页面切换
     * @param postion
     */
    public void changeView(int postion){
        switch (postion) {
            case BOTTOM_VIP:
                bottomVipView.setChecked(true);
                break;
            case BOTTOM_CAR:
                bottomCarView.setChecked(true);
                break;
            case BOTTOM_SHOP:
                bottomShopView.setChecked(true);
                break;
            case BOTTOM_HOME:
                bottomHomeView.setChecked(true);
                break;
        }
        setFragments(postion);
    }
    BaseFragment mContent;
    /** 初始化显示内容 **/
    private void initContent() {
        homeFragment = new HomeFragment();
        vipFragment = new UserFragment();
        shopFragment = new ShopFragment();
        carFragment = new CarFragment();
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.add(R.id.pager_frame, vipFragment);
        transaction.add(R.id.pager_frame, shopFragment);
        transaction.add(R.id.pager_frame, carFragment);
        transaction.add(R.id.pager_frame, homeFragment);
        mContent = homeFragment;
        transaction.commit();
    }
    /**
     * 设置下导航内容
     *
     * @param position Navigation tab position.
     */
    public void setFragments(int position) {

        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        switch (position){
            case BOTTOM_HOME:{
                if (homeFragment == null) {
                    homeFragment = new HomeFragment();
                }
                transaction.replace(R.id.pager_frame, homeFragment);
                break;
            }
            case BOTTOM_VIP:{
                if (vipFragment == null) {
                    vipFragment = new UserFragment();
                }
                transaction.replace(R.id.pager_frame, vipFragment);
                break;
            }
            case BOTTOM_CAR:{
                if (carFragment == null) {
                    carFragment = new CarFragment();
                }
                transaction.replace(R.id.pager_frame, carFragment);
                break;
            }
            case BOTTOM_SHOP:{
                if (shopFragment == null) {
                    shopFragment = new ShopFragment();
                }
                transaction.replace(R.id.pager_frame, shopFragment);
                break;
            }
        }
        transaction.commitAllowingStateLoss();
    }
    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
        if (!b) {
            return;
        }
        switch (compoundButton.getId()) {
            case R.id.navigation_tabs_home: {
                BOTTOM_TYPE = BOTTOM_HOME;

                changeView(BOTTOM_TYPE);
                break;
            }
            case R.id.navigation_tabs_shop: {
                BOTTOM_TYPE = BOTTOM_SHOP;

                changeView(BOTTOM_TYPE);
                break;
            }
            case R.id.navigation_tabs_car: {
                if (TextUtils.isEmpty(UserConfig.getConfigUserId())) {
                    //登录
                    Intent intent = new Intent(this, LoginActivity.class);
                    startActivity(intent);
                } else {
                    BOTTOM_TYPE = BOTTOM_CAR;
                    changeView(BOTTOM_TYPE);
                }
                break;
            }
            case R.id.navigation_tabs_vip: {
                if (TextUtils.isEmpty(UserConfig.getConfigUserId())) {
                    //登录
                    Intent intent = new Intent(this, LoginActivity.class);
                    startActivity(intent);
                } else {
                    BOTTOM_TYPE = BOTTOM_VIP;
                    changeView(BOTTOM_TYPE);
                }

                break;
            }
        }
    }
    private long exitTime = 0;
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode==KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0){

            if (BOTTOM_TYPE!=BOTTOM_HOME){
                BOTTOM_TYPE = BOTTOM_HOME;
                bottomHomeView.setChecked(true);
                bottomVipView.setChecked(false);
                setFragments(BOTTOM_TYPE);

            }else{
                if((System.currentTimeMillis()-exitTime) > 2000){
                    LSCToast.show(MyApplication.getContext(), "再按一次退出程序");
                    exitTime = System.currentTimeMillis();
                } else {
                    AppManager.getAppManager().AppExit();
                    System.exit(0);
                }
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
